package hang.study.uistudy;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hang on 2015-9-13.
 */
public class TestImgActivity extends Activity {

    //图片数组
    int[] images = new int[]{
            R.drawable.a1,
            R.drawable.a2,
            R.drawable.a3,
            R.drawable.a4,
            R.drawable.a5,
            R.drawable.a6
    };
    //默认显示的图片
    private int currentImg = 2;
    private int alpha = 255;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_img_layout);

        final Button btnPlus = (Button)findViewById(R.id.btnPlus);
        final Button btnMinus = (Button)findViewById(R.id.btnMinus);
        final Button btnNext = (Button)findViewById(R.id.btnNext);
        final ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
        final ImageView imageView2 = (ImageView)findViewById(R.id.imageView2);

        /*
        消息处理
         */
        final Handler handler = new Handler(){

            @Override
            public void handleMessage(Message msg) {
                if(msg.what == 0x123){
                    imageView1.setImageResource(images[++currentImg%images.length]);
                }
            }
        };

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                //handler.sendEmptyMessage(0x123);
            }
        },0,1200);

        final CalThr thr = new CalThr();
        thr.start();


        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //imageView1.setImageResource(images[++currentImg%images.length]);

                //创建消息
                Message msg = new Message();
                msg.what = 123;
                Bundle bundle = new Bundle();
                bundle.putInt("123",456);
                msg.setData(bundle);
                thr.mHandler.sendMessage(msg);
            }
        });

        View.OnClickListener alphaListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v == btnPlus){
                    alpha += 20;
                }
                if(v == btnMinus){
                    alpha -= 20;
                }

                if(alpha > 255){
                    alpha = 255;
                }
                if(alpha < 0){
                    alpha = 0;
                }
                imageView1.setImageAlpha(alpha);
            }
        };

        btnMinus.setOnClickListener(alphaListener);
        btnPlus.setOnClickListener(alphaListener);

        imageView1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable)imageView1.getDrawable();
                Bitmap bitMap = bitmapDrawable.getBitmap();
                double scale = bitMap.getWidth()/320.0;
                int x = (int)(event.getX()*scale);
                int y = (int)(event.getY()*scale);
                if(x+120 > bitMap.getWidth()){
                    x = bitMap.getWidth()-120;
                }
                if(y+120 > bitMap.getHeight()){
                    y = bitMap.getHeight()-120;
                }
                imageView2.setImageBitmap(Bitmap.createBitmap(bitMap,x,y,120,120));
                imageView2.setAlpha(alpha);
                return false;
            }
        });
    }

    //定义线程类
    class CalThr extends Thread{

        public Handler mHandler;

        @Override
        public void run() {
            Looper.prepare();
            mHandler = new Handler(){
                @Override
                public void handleMessage(Message msg){
                    if(msg.what == 123){
                        int aa = msg.getData().getInt("123");
                        Toast.makeText(TestImgActivity.this,"消息到了:"+aa,Toast.LENGTH_LONG).show();
                    }
                }
            };

            Looper.loop();
        }
    }
}
